﻿namespace LuckyAssetManagerContrib.FileCache
{
	using System;
	using System.IO;
	using System.Web;
	using System.Xml.Serialization;

	/// <summary>
	/// File cache item
	/// </summary>
	[XmlRoot("item")]
	public sealed class AssetFileCacheItem
	{
		/// <summary>
		/// Absolute path to file cache directory
		/// </summary>
		private readonly string _assetFileCacheDirectoryPath;

		/// <summary>
		/// Cache item key
		/// </summary>
		[XmlAttribute("key")]
		public string Key
		{
			get; 
			set;
		}

		/// <summary>
		/// Name of temporary file, which stores text 
		/// content of processed asset
		/// </summary>
		[XmlAttribute("tempFileName")]
		public string TempFileName
		{
			get;
			set;
		}

		/// <summary>
		/// List of file paths associated with processed asset
		/// </summary>
		[XmlArray("associatedFilePaths")]
		[XmlArrayItem("associatedFilePath")]
		public string[] AssociatedFilePaths
		{
			get;
			set;
		}

		/// <summary>
		/// Date and time of last modification files associated 
		/// with processed asset
		/// </summary>
		[XmlAttribute("lastModifyTime")]
		public DateTime LastModifyTime
		{
			get;
			set;
		}

		/// <summary>
		/// Date and time of creation file cache item
		/// </summary>
		[XmlAttribute("creationTime")]
		public DateTime CreationTime
		{
			get;
			set;
		}

		/// <summary>
		/// Date and time of last access to file cache item
		/// </summary>
		[XmlAttribute("lastAccessTime")]
		public DateTime LastAccessTime
		{
			get;
			set;
		}


		public AssetFileCacheItem()
		{
			_assetFileCacheDirectoryPath = HttpContext.Current.Server.MapPath(
				Constants.AssetFileCacheDirectoryPath);
		}


		/// <summary>
		/// Gets path to temporary file
		/// </summary>
		/// <returns>Path to temporary file</returns>
		private string GetTempFilePath()
		{
			return Path.Combine(_assetFileCacheDirectoryPath, TempFileName);
		}

		/// <summary>
		/// Reads text content of temporary file
		/// </summary>
		/// <returns>Text content of temporary file</returns>
		public string ReadTempFileContent()
		{
			string content = null;
			string filePath = GetTempFilePath();

			if (File.Exists(filePath))
			{
				using (var file = new StreamReader(filePath))
				{
					content = file.ReadToEnd();
				}
			}

			return content;
		}

		/// <summary>
		/// Writes text content into temporary file
		/// </summary>
		/// <param name="content">Text content</param>
		internal void WriteTempFileContent(string content)
		{
			if (!Directory.Exists(_assetFileCacheDirectoryPath))
			{
				Directory.CreateDirectory(_assetFileCacheDirectoryPath);
			}

			string filePath = GetTempFilePath();

			using (var file = new StreamWriter(filePath))
			{
				file.Write(content);
			}
		}

		/// <summary>
		/// Removes temporary file
		/// </summary>
		internal void RemoveTempFile()
		{
			string filePath = GetTempFilePath();

			if (File.Exists(filePath))
			{
				File.Delete(filePath);
			}
		}
	}
}
